package cn.itcast.controller.cargo;

import cn.itcast.base.BaseController;
import cn.itcast.domain.cargo.Finance;
import cn.itcast.domain.cargo.FinanceExample;
import cn.itcast.domain.cargo.InvoiceExample;
import cn.itcast.service.cargo.FinanceService;
import cn.itcast.service.cargo.InvoiceService;
import cn.itcast.service.company.CompanyService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping(value = "/cargo/finance")
public class FinanceController extends BaseController {

    @Reference
    private FinanceService financeService;
    @Reference
    private CompanyService companyService;
    @Reference
    private InvoiceService invoiceService;

    @RequestMapping(value = "/list",name = "合同的分页数据查询")
    public String findAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "4") int pageSize){
        FinanceExample example = new FinanceExample();
        // 根据合同创建时间排序
        example.setOrderByClause("create_time desc");
        PageInfo page = financeService.findAll(example, pageNum, pageSize);
        request.setAttribute("page",page);
        return "cargo/finance/finance-list";
    }

    @RequestMapping(value = "/toAdd",name = "合同的新增页面")
    public String toAdd(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "4") int pageSize){
        InvoiceExample invoiceExample = new InvoiceExample();
        InvoiceExample.Criteria criteria = invoiceExample.createCriteria();
        criteria.andStateEqualTo(1);
        PageInfo page = invoiceService.findAll(invoiceExample, pageNum, pageSize);
        request.setAttribute("page", page);
        return "cargo/finance/finance-add";
    }

    @RequestMapping(value = "/toUpdate",name = "发票编辑页面")
    public String toUpdate(String id){
        // 获取要修改的发票
        Finance finance=financeService.findById(id);
        request.setAttribute("finance",finance);

        return "cargo/finance/finance-update";
    }

    @RequestMapping(value = "/edit",name = "新增发票单信息")
    public String edit(Finance finance,String financeId){
        finance.setFinanceId(financeId);
        financeService.save(finance);
        return "redirect:/cargo/finance/list.do";
    }

    @RequestMapping(value = "/update", name = "编辑委托单")
    public String update(Finance finance) {
        financeService.update(finance);
        return "redirect:/cargo/finance/list.do";
    }


    @RequestMapping(value = "/delete",name = "合同的删除")
    public String del(String id){
        financeService.delete(id);
        // 到最新页面展示最新数据
        return "redirect: /cargo/finance/list.do";
    }


    @RequestMapping(value = "/toView",name = "合同查看页面")
    public String toView(String id){
        // 获取要修改的合同
        Finance finance=financeService.findById(id);
        request.setAttribute("finance",finance);

        return "cargo/finance/finance-view";
    }



    @RequestMapping(value = "/submit",name = "合同的提交")
    public String submit(String id){

        // 根据id获取合同
        Finance finance = financeService.findById(id);
        // 修改这个合同的state状态
        finance.setState(1);
        // 修改合同
        financeService.update(finance);
        // 到最新页面展示最新数据
        return "redirect: /cargo/finance/list.do";
    }

    @RequestMapping(value = "/cancel",name = "合同的取消")
    public String cancel(String id){

        // 根据id获取合同
        Finance finance = financeService.findById(id);
        // 修改这个合同的state状态
        finance.setState(0);
        // 修改合同
        financeService.update(finance);
        // 到最新页面展示最新数据
        return "redirect: /cargo/finance/list.do";
    }


}
